package com.turtle3d.gui.fractaloptions;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.table.AbstractTableModel;

public class ParameterTableModel extends AbstractTableModel
{
	
	private Hashtable<String, Float> parameters;
	private ArrayList<String> keys;
	
	public ParameterTableModel(Hashtable<String, Float> parameters)
	{
		refresh(parameters);
		
	}
	
	public void refresh(Hashtable<String, Float> parameters){
		this.parameters = parameters;
		keys = new ArrayList<String>();
		
		//to fix key order
		//what if new keys appear...?
		for(String key: parameters.keySet())
			keys.add(key);
		
		fireTableDataChanged();
	}

	public int getColumnCount()
	{		
		return 2;
	}

	public int getRowCount()
	{
		return keys.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex)
	{				
		
		try
		{
			if(columnIndex==0)
				return keys.get(rowIndex);
			if(columnIndex==1)
				return parameters.get(keys.get(rowIndex));
					
			return null;
		}
		catch (IndexOutOfBoundsException e)
		{ 
			e.printStackTrace();
			return null;
		}
		
	}
	
	 public boolean isCellEditable(int row, int col) {
		return col==1; 
	 }
	 
	 public void setValueAt(Object value, int row, int col) {
		 
		 try
		{
			float f= Float.parseFloat(value.toString());
			 
			parameters.put(keys.get(row), f);
			
			fireTableCellUpdated(row, col);
		}
		catch (NumberFormatException e)
		{
			
		}
		 
	 }
	 
	@Override
	public String getColumnName(int column)
	{
		if(column==0)
			return "Parameter";
		else
			return "Value";
	}

}
